﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PowerManager
{
	public class EventTime
	{
		private int seconds;
		public int Seconds
		{
			get { return seconds; }
		}

		private int minutes;
		public int Minutes
		{
			get { return minutes; }
		}

		private int hours;
		public int Hours
		{
			get { return hours; }
		}

		public int TotalSeconds
		{
			get
			{
				return this.Hours * 3600 + this.Minutes * 60 + this.Seconds;
			}
		}

		public static EventTime GetTimeBySeconds(int seconds)
		{
			if (seconds < 0)
				return new EventTime(0, 0, 0);

			int minutes = 0;
			int hours = 0;
			
			if (seconds > 59)
			{
				minutes = (seconds / 60);
				if (minutes > 59)
				{
					hours = (minutes / 60);
					if (hours > 99)
						return new EventTime(99, 59, 59);

					minutes %= 60;
				}
				seconds %= 60;
			}

			return new EventTime(hours, minutes, seconds);
		}

		public static EventTime operator +(EventTime time, int seconds)
		{
			int totalSeconds = time.TotalSeconds + seconds;
			return GetTimeBySeconds(totalSeconds);
		}

		public static EventTime operator -(EventTime time, int seconds)
		{
			int totalSeconds = time.TotalSeconds - seconds;			
			return GetTimeBySeconds(totalSeconds);
		}

		public static EventTime operator ++(EventTime time)
		{
			int totalSeconds = time.TotalSeconds + 1;
			return GetTimeBySeconds(totalSeconds);
		}

		public static EventTime operator --(EventTime time)
		{
			int totalSeconds = time.TotalSeconds - 1;
			return GetTimeBySeconds(totalSeconds);
		}

		public static EventTime operator *(EventTime time, int times)
		{
			int totalSeconds = time.TotalSeconds * times;
			return GetTimeBySeconds(totalSeconds);
		}

		public static EventTime operator /(EventTime time, int times)
		{
			int totalSeconds = time.TotalSeconds / times;
			return GetTimeBySeconds(totalSeconds);
		}

		public override bool Equals(object obj)
		{
			EventTime time = obj as EventTime;
			return this.TotalSeconds == time.TotalSeconds;
		}

		public static bool operator ==(EventTime time, int seconds)
		{
			return GetTimeBySeconds(seconds) == time;
		}

		public static bool operator !=(EventTime time, int seconds)
		{
			return GetTimeBySeconds(seconds) != time;
		}

		public override string ToString()
		{
			return String.Format("{0:00}", this.Hours) + ':'
				 + String.Format("{0:00}", this.Minutes) + ':'
				 + String.Format("{0:00}", this.Seconds);
		}

		public EventTime(int hours, int minutes, int seconds)
		{
			this.hours = hours;
			this.minutes = minutes;
			this.seconds = seconds;
		}
	}
}
